<?php
namespace Resmodel;



use Core\Config;
use Core\Service\Parameterchooser\ParamableInterface;
use Resmodel\Factory\ResTableGatewayFactory;


class MlCategory extends ResAbstractModel implements ParamableInterface
{
    public $id;
    public $code;
    public $lft;
    public $rgt;
    public $group_code;
    public $can_delete;
    public $active;


    public function getMlCategoryLang($lang){
        $mlCategoryLangTable = ResTableGatewayFactory::getMlCategoryLangTable();
        $select = $mlCategoryLangTable->getDbSqlSelect();
        $select->where(array('category_id'=>$this->id, 'lang'=>$lang));
        $arr = $mlCategoryLangTable->executeSelectForArray($select);
        if($arr && count($arr) > 0)
            return $arr[0];
    }

    /**
     * @return string
     */
    public function getSpecification()
    {
        $defaultLanguage = Config::getDefaultLanguage();
        $mlCategoryLang = $this->getMlCategoryLang($defaultLanguage);
        return $mlCategoryLang->name;
    }

    /**
     * @return string
     */
    public function getParameter()
    {
        $defaultLanguage = Config::getDefaultLanguage();
        $mlCategoryLang = $this->getMlCategoryLang($defaultLanguage);
        return $mlCategoryLang->alias.'_'.$this->id;
    }


    public function getDirectParent(){
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $ancestorRows = $categoryTable->getTableGateway()->select(array(
            'lft < '.$this->lft,
            'rgt > '.$this->rgt,
            'group_code'=>$this->group_code,
        ));
        if($ancestorRows->count() == 0)
            return null;
        $minLft = 0;
        $result = null;
        foreach ($ancestorRows as $ancestor) {
            if($ancestor->lft > $minLft){
                $minLft = $ancestor->lft;
                $result = $ancestor;
            }
        }

        return $result;
    }

    public function getAllChildren(){
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        return $categoryTable->getTableGateway()->select(array(
            'lft > '.$this->lft,
            'rgt < '.$this->rgt,
            'group_code'=>$this->group_code,
        ));

    }

    public function upMyPosition(){

        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $upperBrother = $categoryTable->getTableGateway()->select(array(
            'rgt'=>($this->lft - 1),
            'group_code'=>$this->group_code,
        ))->current();
        $leftDelta = $this->lft - $upperBrother->lft;
        $rightDelta =  $this->rgt - $upperBrother->rgt;
        $myChildren = $this->getAllChildren();
        $upperBrother->setDomain($this->getDomain());
        $upperBrotherTree = $upperBrother->getAllChildren();
        foreach ($myChildren as $item) {
            $categoryTable->update(array('lft'=>($item->lft - $leftDelta),
                'rgt'=>($item->rgt - $leftDelta),), $item->id);
        }
        $categoryTable->update(array('lft'=>($this->lft - $leftDelta),
            'rgt'=>($this->rgt - $leftDelta),), $this->id);

        foreach ($upperBrotherTree as $item) {
            $categoryTable->update(array('lft'=>($item->lft + $rightDelta),
                'rgt'=>($item->rgt + $rightDelta),), $item->id);
        }

        $categoryTable->update(array('lft'=>($upperBrother->lft + $rightDelta),
            'rgt'=>($upperBrother->rgt + $rightDelta)), $upperBrother->id);
    }

    public function downMyPosition(){
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $lowerBrother = $categoryTable->getTableGateway()->select(array(
            'lft'=>($this->rgt + 1),
            'group_code'=>$this->group_code,

        ))->current();
        $lowerBrother->setDomain($this->getDomain());
        $lowerBrother->upMyPosition();
    }


    public function getComboTree(){
        $categoryTable = ResTableGatewayFactory::getMlCategoryTable();
        $rows = $categoryTable->getAllCategoriesByGroupCode($this->group_code);
        $right = array();
        $result = array();

        foreach ($rows as $aMlCategory) {
            $item = (array)$aMlCategory;
            if(count($right)>0){
                while ($right[count($right)-1]['rgt']<$item['rgt']) {
                    array_pop($right);
                }
            }
            foreach ($right as $rightItem) {
                $item["name"] = "-".$item["name"];
            }
            array_push($result, $item);

            array_push($right,$item);
        }
        return $result;
    }


    public function isLeaf()
    {
        if($this->lft == ($this->rgt - 1))
            return true;
        return false;
    }
}
